package com.atguigu.spzx.cloud.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.atguigu.spzx.cloud.user.entity.User;
import com.atguigu.spzx.cloud.user.service.UserService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserService userService;

    @Value("${server.port}")
    String port;

    @SentinelResource("hot")  // 声明资源名称
    //远程接口：被订单微服务调用
    @GetMapping("findUserByUserId/{id}")
    public User findUserByUserId(@PathVariable("id") Long id,
                                 @RequestHeader(value="company",required = false,defaultValue = "BAT") String company,
                                 @RequestParam(value="email",required = false,defaultValue = "xx@163.com") String email,
                                 @RequestHeader(value="age",required = false,defaultValue = "0") Integer age,
                                 @RequestHeader(value="token",required = false,defaultValue = "a.b.c") String token) {
        System.out.println("company = " + company);
        System.out.println("age = " + age);
        System.out.println("email = " + email);
        System.out.println("调用者的拦截器传递的头 token = " + token);
        //System.out.println("user服务的findUserByUserId接口被调用了....");
        /*try {
            TimeUnit.SECONDS.sleep(61);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        User user = userService.findUserByUserId(id);
        user.setUserName(user.getUserName()+" " + port);
        return user; //转换为json返回
    }



    // 修改用户数据端点
    @GetMapping(value = "/updateUserById")
    public String updateUserById() {
        return "修改用户数据成功";
    }



    @GetMapping(value = "/save")
    public String save() {
        userService.queryUsers();
        System.out.println("保存用户");
        return "订单保存成功" ;
    }

    @GetMapping(value = "/query")
    public String query() {
        userService.queryUsers();
        System.out.println("查询用户");
        return "查询用户成功" ;
    }
}
